Shader "Custom/SphereOutline"
{
Properties
{
    _MainTex ("Main Texture", 2D) = "white" {}
    _OutlineColor ("Outline Color", Color) = (1,1,1,1)
}
SubShader
{
    Cull Back //剔除背面
    pass //贴图
    {
       CGPROGRAM
       #pragma vertex vert
       #pragma fragment frag
       #include "UnityCG.cginc"

       struct a2v
       {
           float4 pos : POSITION;  //语义修饰，模型空间的顶点的位置
           float2 uv  : TEXCOORD0; //纹理uv
       };

       struct v2f
       {
           float4 svpos : SV_POSITION; //裁剪空间顶点位置
           float2 svuv : TEXCOORD0;    //纹理
       };


       sampler2D _MainTex;

       v2f vert(a2v i)
       {
           v2f o;
           o.svpos = UnityObjectToClipPos(i.pos);
           o.svuv = i.uv;
           return o;
       }

       float4 frag(v2f u) : SV_TARGET
       {
           float4 tex = tex2D(_MainTex, u.svuv);
           return tex;
       }

       ENDCG
    }

    //添加outline
    //"RenderType" = "Transparent"
    Tags{
        "Queue" = "Transparent"
        "RenderType" = "Transparent"
    }
    pass
    {
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Front
        CGPROGRAM
        
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        struct a2v
        {
            float4 pos : POSITION;  //语义修饰，模型空间的顶点的位置
            float3 normal : NORMAL; //法线
        };

        struct v2f
        {
            float4 svpos : SV_POSITION; //裁剪空间顶点位置
            float3 normal : NORMAL; //法线
            float3 vertworldPos : TEXCOORD1; //顶点在世界空间下的点
        };

        float4 _OutlineColor;

        v2f vert(a2v i)
        {
            v2f o;
            i.pos.xyz += i.normal*0.1;
            o.svpos = UnityObjectToClipPos(i.pos);
            o.normal = i.normal;
            o.vertworldPos = mul(unity_ObjectToWorld, i.pos).xyz;
            return o;
        }

        float4 frag(v2f u) : COLOR
        {
            u.normal = normalize(u.normal);
            float3 viewDir = normalize(u.vertworldPos.xyz - _WorldSpaceCameraPos.xyz);
            float dots = pow(dot(u.normal, viewDir),3);
            float4 color = _OutlineColor;
            color.a = dots;
            color.a *= 10;
            return color;
        }

        ENDCG
    }

}
}
